package com.demo2do.boyle.backend.service.customer.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.backend.service.customer.WalletService;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.entity.customer.wallet.Wallet;
import com.demo2do.boyle.common.entity.customer.wallet.WalletRecord;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.demo2do.core.web.resolver.Page;

/**
 * 
 * @author Wilson
 */
@Service("walletService")
public class WalletServiceImpl extends GenericServiceImpl<Wallet> implements WalletService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.service.WalletService#load(com.demo2do.boyle.common.entity.customer.Customer)
     */
    @SuppressWarnings("unchecked")
    public Wallet load(Customer customer) {

        List<Wallet> wallets = genericDaoSupport.searchForList("FROM Wallet WHERE disabled = false AND customer = :customer", "customer", customer);

        return CollectionUtils.isEmpty(wallets) ? null : wallets.get(0);
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.service.WalletService#listRecords(com.demo2do.boyle.common.entity.customer.wallet.Wallet)
     */
    @SuppressWarnings("unchecked")
    public List<WalletRecord> listRecords(Wallet wallet, Page page) {
    	
        List<WalletRecord> walletRecords = genericDaoSupport.searchForList( "FROM WalletRecord WHERE disabled = false AND wallet = :wallet", "wallet", wallet, page.getBeginIndex(), page.getMaxResultRecords() );
		page.calculate(walletRecords.size());
		return walletRecords.size() > page.getEveryPage() ? walletRecords.subList(0, page.getEveryPage()) : walletRecords;
    }

}
